package com.cjjd.c.feature.subscribe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.BaseFragment;
import com.cjjd.c.feature.subscribe.near_hotel.HotelNearFragment;
import com.cjjd.c.feature.subscribe.nomal_hotel.HotelNomalFragment;
import com.cjjd.c.feature.subscribe.select_hotel.HotelSelectFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ----------------------------------
 * Created by xyd on 2017/11/22.
 * <p/>
 * 描  述：会员预订
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class HotelSubscribFragment extends BaseFragment {
    @Bind(R.id.rg_tab)
    RadioGroup rgTab;
    @Bind(R.id.fragment_hotel_select)
    FrameLayout fragmentHotelSelect;
    @Bind(R.id.rb_1)
    RadioButton rb1;
    @Bind(R.id.tv_hotel_title)
    TextView tvHotelTitle;


    @Override
    public int getContentView() {
        return R.layout.fragment_hotel_subscribe;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {

        rb1.setChecked(true);
        changeTab(R.id.rb_1);
        rgTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_1:
                        changeTab(R.id.rb_1);
                        break;
                    case R.id.rb_2:
                        changeTab(R.id.rb_2);
                        break;
                    case R.id.rb_3:
                        changeTab(R.id.rb_3);
                        break;
                }
            }
        });

    }

    private Fragment hotelNomalFragment;
    private Fragment hotelNearFragment;
    private Fragment hotelSelectFragment;

    private void changeTab(int res_id) {


        if (res_id == R.id.rb_1) {

            if (hotelNearFragment != null) {
                getChildFragmentManager().beginTransaction().hide(hotelNearFragment).commit();
            }

            if (hotelSelectFragment != null) {
                getChildFragmentManager().beginTransaction().hide(hotelSelectFragment).commit();
            }

            if (hotelNomalFragment == null) {
                hotelNomalFragment = new HotelNomalFragment();
                //fragmentHotelSelect.addView(hotelNomalFragment);
                getChildFragmentManager().beginTransaction().add(R.id.fragment_hotel_select, hotelNomalFragment, "hotel_noaml").commitNow();
                getChildFragmentManager().beginTransaction().show(hotelNomalFragment).commit();
            } else {
                getChildFragmentManager().beginTransaction().show(hotelNomalFragment).commit();
            }

        } else if (res_id == R.id.rb_2) {

            if (hotelNomalFragment != null) {
                getChildFragmentManager().beginTransaction().hide(hotelNomalFragment).commitNow();
            }

            if (hotelSelectFragment != null) {
                getChildFragmentManager().beginTransaction().hide(hotelSelectFragment).commitNow();
            }

            if (hotelNearFragment == null) {
                hotelNearFragment = new HotelNearFragment();
                //fragmentHotelSelect.addView(hotelNomalFragment);
                getChildFragmentManager().beginTransaction().add(R.id.fragment_hotel_select, hotelNearFragment, "hotel_near").commitNow();
                getChildFragmentManager().beginTransaction().show(hotelNearFragment);
            } else {
                getChildFragmentManager().beginTransaction().show(hotelNearFragment).commitNow();
            }
        } else if (res_id == R.id.rb_3) {

            if (hotelNomalFragment != null) {
                getChildFragmentManager().beginTransaction().hide(hotelNomalFragment).commitNow();
            }
            if (hotelNearFragment != null) {
                getChildFragmentManager().beginTransaction().hide(hotelNearFragment).commitNow();
            }
            if (hotelSelectFragment == null) {
                hotelSelectFragment = new HotelSelectFragment();
                getChildFragmentManager().beginTransaction().add(R.id.fragment_hotel_select, hotelSelectFragment, "hotel_select").commitNow();
                getChildFragmentManager().beginTransaction().show(hotelSelectFragment);
            } else {
                getChildFragmentManager().beginTransaction().show(hotelSelectFragment).commitNow();
            }
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.tv_hotel_title)
    public void onViewClicked() {
        Intent intent = new Intent(mContext, HotelSearchActivity.class);
        mContext.startActivity(intent);

    }
}
